# Title     : TODO
# Objective : TODO
# Created by: yz
# Created on: 4/9/2020

library(jsonlite)

getFinalWidth <- function(width, defaultWidth, pcFile) {
  if (width == 0) {
    plotConfigJson <- fromJSON(pcFile)
    plotConfigJson$layout$width<-as.character(defaultWidth)
    write_json(plotConfigJson,pcFile,auto_unbox =TRUE)
    defaultWidth
  }else {
    width
  }
}

getFinalHeight <- function(height, defaultHeight, pcFile) {
  if (height == 0) {
    plotConfigJson <- fromJSON(pcFile)
    plotConfigJson$layout$height<-as.character(defaultHeight)
    write_json(plotConfigJson,pcFile,auto_unbox =TRUE)
    defaultHeight
  }else {
    height
  }
}

getfinalColors <- function(plotConfigData) {
  colorsStr <- plotConfigData$color$colorsStr
  customColors <- plotConfigData$color$customColors
  finalColorsStr <- if (colorsStr == "custom") {
    customColors
  } else {
    colorsStr
  }
  finalColors <- str_split(finalColorsStr, ":") %>%
    unlist()
  finalColors
}

plotConfigJson <- fromJSON(opt$pc)
print(plotConfigJson)

x <- plotConfigJson$layout$x
y <-plotConfigJson$layout$y
z <-plotConfigJson$layout$z
width <- plotConfigJson$layout$width %>%
  as.numeric()
height <-plotConfigJson$layout$height %>%
  as.numeric()
showSample <- plotConfigJson$layout$showSample %>%
  as.logical()
showEllipse <-plotConfigJson$layout$showEllipse %>%
  as.logical()
xFont <- plotConfigJson$font$xFont
xTitleFont <- plotConfigJson$font$xTitleFont
yFont <- plotConfigJson$font$yFont
yTitleFont <- plotConfigJson$font$yTitleFont
mainTitle <-plotConfigJson$font$mainTitle
mainTitleFont <-plotConfigJson$font$mainTitleFont
legendFont <-plotConfigJson$font$legendFont
legendTitle <- plotConfigJson$font$legendTitle
legendTitleFont <-plotConfigJson$font$legendTitleFont
r2xColor <- plotConfigJson$color$r2x
r2yColor <-plotConfigJson$color$r2y
q2Color <-plotConfigJson$color$q2
q2yColor <- plotConfigJson$color$q2y
rowCluster <- plotConfigJson$layout$rowCluster %>%
  as.logical()
rowClusterMethod <- plotConfigJson$layout$rowClusterMethod
colCluster <- plotConfigJson$layout$colCluster %>%
  as.logical()
colClusterMethod <- plotConfigJson$layout$colClusterMethod
colorsStr <- plotConfigJson$color$colorsStr
customColors <- plotConfigJson$color$customColors
finalColorsStr <- if (!is.null(colorsStr)  && colorsStr == "custom") {
  customColors
} else {
  colorsStr
}
finalColors <- str_split(finalColorsStr, ":") %>%
  unlist()
naColor <- plotConfigJson$color$naColor
showRowName <- plotConfigJson$font$showRowName %>%
  as.logical()
showColName <- plotConfigJson$font$showColName %>%
  as.logical()
xRotate <- plotConfigJson$font$xRotate %>%
  as.numeric()
yRotate <- plotConfigJson$font$yRotate %>%
  as.numeric()
rowTreeHeight <- plotConfigJson$layout$rowTreeHeight %>%
  as.numeric()
colTreeHeight <- plotConfigJson$layout$colTreeHeight %>%
  as.numeric()
fontFamily <-plotConfigJson$font$fontFamily

print(showSample)

getBasicPlotArg <- function(p) {
  p <- p +
    theme(plot.title = element_text(hjust = 0.5, size = mainTitleFont), legend.text = element_text(size = legendFont),
          legend.title = element_text(size = legendTitleFont), axis.text.x = element_text(size = xFont),
          axis.title.x = element_text(size = xTitleFont), axis.text.y = element_text(size = yFont),
          axis.title.y = element_text(size = yTitleFont))

  if (!is.na(mainTitle)) {
    p <- p + ggtitle(mainTitle)
  }

  p
}

